//
//  CZWelcomeViewController.swift
//  01-sinaWeibo-05-09
//
//  Created by mac on 16/5/15.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class CZWelcomeViewController: UIViewController {

    override func viewDidLoad()
    {
        super.viewDidLoad()
      
        prepareUI()
          self.setIcon()
        // 即使头像在本地缓存了图片,刚开始会用占位图片,只有网路请求完成后才会使用SDWebImage设置图片
        // 解决方法,在获取用户信息之前,也使用SDWebImage去设置一下头像的图片

        //获取用户信息
        CZUserAccountViewModel.sharedInstance.laodUserInfo { () -> () in
            self.setIcon()
        }
    }
    
    private func setIcon(){
    
        if let avatar_large = CZUserAccountViewModel.sharedInstance.userAccount?.avatar_large{
        
            let url = NSURL(string: avatar_large)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        moveAnimation()
    }
    
    //移动的动画
    private func moveAnimation()
    {
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height-160))
                
            })
           self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                CZPrint(items: "动画完成")
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                        appDelegate.switchRootViewController(CZMainTabBarController())
                })
        }
    }
    
    private func prepareUI()
    {
        //self.view 添加子控件
        view.addSubview(bkgView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        //autolayout
        //背景
        //bkgView : 要添加约束的view
        //snp_makeConstraints:要添加约束
        //make:要添加约束的view
        bkgView.snp_makeConstraints { (make) -> Void in
            //bkgView的边缘等于self.view的边缘
            make.edges.equalTo(self.view)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        
    }
    
    
    //MARK: - 懒加载
    //背景
    private lazy var bkgView:UIImageView = UIImageView(image: UIImage(named:"ad_background" ))
    
    //头像
    private lazy var iconView:UIImageView =
    {
        let imageView = UIImageView(image: UIImage(named:"avatar_default_big"))
        
        // 圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    //消息
    private lazy var messageLabel:UILabel =
    {
        var label = UILabel()
        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(15)
        return label
        
    }()

}
